<template>
  <div class="finance">
    <el-form
      ref="settingForm"
      class="setting-form"
      :rules="formDataRules"
      :model="formData"
      label-position="left"
      label-width="160px"
    >
      <el-form-item label="默认红包手续费" prop="default_red_ratio">
        <el-input v-model.number="formData.default_red_ratio" class="input">
          <div slot="append">%</div>
        </el-input>
      </el-form-item>
      <el-form-item label="图片红包手续费" prop="image_red_ratio">
        <el-input v-model.number="formData.image_red_ratio" class="input">
          <div slot="append">%</div>
        </el-input>
      </el-form-item>
      <el-form-item label="视频红包手续费" prop="video_red_ratio">
        <el-input v-model.number="formData.video_red_ratio" class="input">
          <div slot="append">%</div>
        </el-input>
      </el-form-item>
      <el-button type="primary" size="small" @click="submit">确认配置</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Finance',
  props: {
    setting: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        default_red_ratio: 0, // 默认红包手续费
        image_red_ratio: 0, // 图片红包手续费
        video_red_ratio: 0 // 视频红包手续费
      },
      formDataRules: {
        default_red_ratio: [
          { type: 'number', min: 0, max: 100, message: '百分比范围0-100' }
        ],
        image_red_ratio: [
          { type: 'number', min: 0, max: 100, message: '百分比范围0-100' }
        ],
        video_red_ratio: [
          { type: 'number', min: 0, max: 100, message: '百分比范围0-100' }
        ]
      }
    }
  },
  watch: {
    setting() {
      const { default_red_ratio, image_red_ratio, video_red_ratio } = this.setting
      this.formData = {
        default_red_ratio,
        image_red_ratio,
        video_red_ratio
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.settingForm.validate((valid) => {
        if (valid) {
          const {
            default_red_ratio,
            image_red_ratio,
            video_red_ratio
          } = this.formData
          this.$put('setting/gain', {
            default_red_ratio,
            image_red_ratio,
            video_red_ratio
          })
            .then(() => {
              this.$message.success({
                message: '修改成功',
                type: 'success'
              })
            })
            .catch((error) => {
              this.$handleError(error)
            })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .setting-form{
    width: 600px;
  }
  .input{
    width: 120px;
  }
</style>
